<?php
/**
 * 后台用控制器模板
 */
namespace app\admin\controller\maintain;

use app\admin\controller\AuthController;
use app\common\model as M;
use service\JsonService;

class RelRecomandRoutes extends AuthController
{
	public $model = '\app\common\model\RelRecomandRoutes';

    use \traits\ShotcutControllerTrait;
	
	public function _save($data)
	{
		if ( !isset($data['recomand_id']) || !isset($data['route_id']) ) throw new \Exception("参数错误");

		## 检查是否已经关联过
		$had = $this->model::where('recomand_id',$data['recomand_id'])->where('route_id',$data['route_id'])->count();
		if ( $had > 0 ) throw new \Exception('已关联过');

		return $data;
	}

	/**
	 * 移除关联
	 */
	public function remove()
	{
		$data = request()->post();
		if ( !isset($data['recomand_id']) || !isset($data['route_id']) ) throw new \Exception("参数错误");

		$res = $this->model::where('recomand_id',$data['recomand_id'])->where('route_id',$data['route_id'])->delete();
		if ( $res === false ) throw new \Exception('移除关联失败');

		return JsonService::successful();
	}

	/**
	 * 排序
	 */
	public function set_sort()
	{
		$data = request()->post();
		if ( !isset($data['recomand_id']) || !isset($data['route_id']) || !isset($data['sort']) ) throw new \Exception("参数错误");

		$res = $this->model::where('recomand_id',$data['recomand_id'])->where('route_id',$data['route_id'])->update(['sort' => $data['sort']]);
		if ( $res === false ) throw new \Exception('排序失败');

		return JsonService::successful();
	}
}// class end
